package com.zuipin.quartz;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.QuartzJobBean;

import com.zuipin.entity.Member;
import com.zuipin.entity.product.EvaluationInfo;
import com.zuipin.entity.product.ProductExtInfo;
import com.zuipin.framework.exception.CustomException;
import com.zuipin.service.IEvaluationInfoService;
import com.zuipin.service.IMemberService;
import com.zuipin.service.IOrderDetailService;
import com.zuipin.service.IOrderService;
import com.zuipin.service.IProductExtInfoService;
import com.zuipin.util.DateUtils;

import net.sf.json.JSONObject;

/**
 * 自动好评
 *
 * @ClassName AutomaticAcclaimJob
 * @author wangcongyi
 * @date 2017年7月10日 下午5:36:18
 */
@Configuration
public class AutomaticAcclaimJob extends QuartzJobBean {
	
	@Resource
	private IProductExtInfoService	productExtInfoService;
	@Resource
	private IEvaluationInfoService	evaluationInfoService;
	@Resource
	private IOrderDetailService		orderDetailService;
	@Resource
	private IOrderService			orderService;
	@Resource
	private IMemberService			memberService;
	private Logger					log				= Logger.getLogger(AutomaticAcclaimJob.class);
	private final static BigDecimal	DEFAUFT_SCORE	= new BigDecimal(100);
	private final static Long		SYS_ID			= 2l;
	private final static Integer	DAY				= 15;
	
	@Override
	protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
		// TODO Auto-generated method stub
		log.info("自动好评--定时器---start");
		List<Map<String, Object>> orders = orderDetailService.quartzCommentProduct(DAY, SYS_ID);
		if (null != orders && !orders.isEmpty()) {
			StringBuilder proSkuBuilder = new StringBuilder();
			String sp = "";
			for (Map<String, Object> map : orders) {
				proSkuBuilder.append(sp);
				proSkuBuilder.append("'");
				proSkuBuilder.append(map.get("proSku"));
				proSkuBuilder.append("'");
				sp = ",";
			}
			for (Map<String, Object> map : orders) {
				Member member = memberService.findById(Long.valueOf(String.valueOf(map.get("createMemberId"))));
				if (null != member) {
					if (null != member.getContactName()) {
						map.put("nickName", member.getContactName());
					} else if (null != member.getPhone()) {
						map.put("nickName", member.getPhone());
					} else if (null != member.getEmail()) {
						map.put("nickName", member.getEmail());
					} else {
						map.put("nickName", "匿名用户");
					}
				} else {
					map.put("nickName", "匿名用户");
				}
			}
			JSONObject params = new JSONObject();
			params.put("proSkus", proSkuBuilder.toString());
			params.put("sysId", SYS_ID);
			List<ProductExtInfo> productExtInfos = productExtInfoService.findProExtBySkus(params);
			if (null != productExtInfos && !productExtInfos.isEmpty()) {
				List<EvaluationInfo> evaluationInfos = new ArrayList<>();
				for (ProductExtInfo productExtInfo : productExtInfos) {
					EvaluationInfo evaluationInfo = new EvaluationInfo();
					for (Map<String, Object> map : orders) {
						if (productExtInfo.getProSku().equals(map.get("proSku"))) {
							evaluationInfo.setContent("默认好评！");
							evaluationInfo.setNickName(String.valueOf(map.get("nickName")));
							evaluationInfo.setProExtId(productExtInfo.getId());
							evaluationInfo.setCreateTime(DateUtils.getCurrentDateTime());
							evaluationInfo.setOrderId(Long.valueOf(String.valueOf(map.get("orderId"))));
							evaluationInfo.setMemberId(Long.valueOf(String.valueOf(map.get("createMemberId"))));
							evaluationInfo.setState(EvaluationInfo.PASS);
							evaluationInfo.setScore(DEFAUFT_SCORE);
							evaluationInfo.setSysId(SYS_ID);
							evaluationInfos.add(evaluationInfo);
						}
					}
					// 更新商品好评数~~~~~~
					productExtInfoService.updateAssessmentNum(SYS_ID, evaluationInfo.getProExtId());
				}
				try {
					evaluationInfoService.insertBatch(evaluationInfos);
					for (EvaluationInfo eval : evaluationInfos) {
						orderService.modityIsComment(eval.getOrderId());
					}
				} catch (CustomException e) {
					// TODO Auto-generated catch block
					log.error("exception description", e);
				}
			}
		}
		log.info("自动好评--定时器---end");
	}
}
